jQuery.noConflict();
(function (PLUGIN_ID) {
	
    "use strict";

    var config = kintone.plugin.app.getConfig(PLUGIN_ID);
    if (!config) { return false; }
    var rowCount = Number(config["rowCount"]);
    var year = {};
    var age = {};
    var pluginDate = {};
    var yearBase = {};
    for (var i = 0; i < rowCount; i++) {
        year[i] = config["year" + i];
        age[i] = config["age" + i];
        pluginDate[i] = config["pluginDate" + i];
        yearBase[i] = config["yearBase" + i];
    }
    var flg = false;
    kintone.events.on('app.record.index.show', function (event) {
        loopGet(0);
    });

    // Loop get more than 100 datas
    function loopGet(offset) {
        var today = moment().format('YYYY-MM-DD');
        var condition = '';
        for (var j = 0; j < rowCount; j++) {
            if (j == rowCount - 1) {
                condition = condition + pluginDate[j] + ' <= "' + today + '" and ' + year[j] + ' != "" or ' + age[j] + ' = "" or ( ' + year[j] + ' = "" and ' + pluginDate[j] + ' != "" ) or ( ' + year[j] + ' = "" and ' + age[j] + ' != 0 )';
            } else {
                condition = condition + pluginDate[j] + ' <= "' + today + '" and ' + year[j] + ' != "" or ' + age[j] + ' = "" or ( ' + year[j] + ' = "" and ' + pluginDate[j] + ' != "" ) or ( ' + year[j] + ' = "" and ' + age[j] + ' != 0 ) or ';
            }
        }
        var param = {
            app: kintone.app.getId(),
            query: condition + ' limit 100 offset ' + offset
        };

        kintone.api(kintone.api.url('/k/v1/records', true), 'GET', param,
            function (resp) {
                var records = resp.records;
                if (records.length != 0) {
                    var json = {};
                    json['app'] = kintone.app.getId();
                    json['records'] = [];
                    for (var i = 0; i < records.length; i++) {
                        var subRec = {};
                        subRec['id'] = records[i]['$id']['value'];
                        subRec['record'] = {};
                        for (var j = 0; j < rowCount; j++) {
                            var startYear = records[i][year[j]]['value'];
                            if (!startYear) {
                                subRec['record'][age[j]] = {};
                                subRec['record'][age[j]]['value'] = 0;
                                subRec['record'][pluginDate[j]] = {};
                                subRec['record'][pluginDate[j]]['value'] = '';
                            } else {
                                var a = moment();
                                var b = moment(startYear);
                                subRec['record'][age[j]] = {};
                                subRec['record'][age[j]]['value'] = Number(a.diff(b, 'years')) + Number(yearBase[j]);
                                subRec['record'][pluginDate[j]] = {};
                                subRec['record'][pluginDate[j]]['value'] = moment(records[i][year[j]]['value']).add(Number(subRec['record'][age[j]]['value']) + 1 - Number(yearBase[j]), 'years').format('YYYY-MM-DD');
                            }
                        }
                        json['records'].push(subRec);
                    }

                    kintone.api(kintone.api.url('/k/v1/records', true), 'PUT', json, function (resp) {
                        flg = true;
                        offset = offset + 100;
                        loopGet(offset);
                        return;
                    }, function (resp) {
                        return;
                    });
                } else {
                    if (flg) {
                        alert("Processing completed!");
                        location.reload();
                    }
                }
            }, function (resp) {
                return;
            });
    }

    kintone.events.on(['app.record.create.show', 'app.record.edit.show',
        'app.record.index.edit.show'], function (event) {
            var record = event.record;
            for (var j = 0; j < rowCount; j++) {
                record[age[j]]['disabled'] = true;
                record[pluginDate[j]]['disabled'] = true;
            }
            return event;
        });

    kintone.events.on(['app.record.create.submit', 'app.record.edit.submit', 'app.record.index.edit.submit'],
        function (event) {
            var record = event.record;
            for (var j = 0; j < rowCount; j++) {
                var startYear = record[year[j]]['value'];
                if (!startYear) {
                    record[age[j]]['value'] = 0;
                    record[pluginDate[j]]['value'] = '';
                } else {
                    var a = moment();
                    var b = moment(startYear);
                    record[age[j]]['value'] = Number(a.diff(b, 'years')) + Number(yearBase[j]);
                    record[pluginDate[j]]['value'] = moment(record[year[j]]['value']).add(Number(record[age[j]]['value']) + 1 - Number(yearBase[j]), 'years').format('YYYY-MM-DD');
                }
            }
            return event;
        });

})(kintone.$PLUGIN_ID);

